const PublicModel = require("../publicmodel");
module.exports = class LineModel extends PublicModel {

	constructor(_db, _id) {
		super(_db, "lg-stock-lines", _id);
		this.symbol = ""; // 股票标识码
		this.day = []; // 日线
		this.week = []; // 周线
		this.month = []; // 月线
		this.create_date; // 创建时间
		this.lastupdate_date; //最后更新时间
	}

	// 获取symbol数组
	async getSymbolList() {
		const {
			data: {
				0: {
					stock_list: stockList = []
				} = {}
			} = []
		} = await this._collection.aggregate().project({
			_id: false,
			symbol: 1
		}).group({
			_id: 1,
			stock_list: this._$.push("$symbol")
		}).end() || {};

		return stockList;
	}
	// async getIterBySybmol(symbol = ""){
	// 	const {data:{0:{ day:{} = [],week:{} = [],month:{} = []} = { }} = []}  = await this._collection.where({symbol}).get();
	// 	return  model;
	// }

	// 推入新的数据
	async pushLineData(symbol = "", lineType = "",data = [],lastupdate_date="") {
		const cmd = this._dbCmd;
		const {updated = 0} = await this._collection.where({symbol}).update({
			[lineType]:cmd.push(data),
			lastupdate_date
		}) || {};
		return updated
	}

	//获取数组最后时间
	async getLastDate(symbol = "") {
		const $ = this._$;
		const {
			data: {
				0: model
			} = []
		} = await this._collection.aggregate().match({
			symbol
		}).addFields({
			day_last: $.arrayElemAt(['$day', -1]),
			week_last: $.arrayElemAt(['$week', -1]),
			month_last: $.arrayElemAt(['$month', -1]),
		}).project({
			_id: 0,
			day: "$day_last.current_date",
			week: "$week_last.current_date",
			month: "$month_last.current_date",
		}).end() || {};
		return model;
	}
	
	
}
